<?php

/**
 * uniBoard ACP Languages page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_langs{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] =  $appCore -> lang['delete_item_sure'];
			
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$appCore -> lang['languages_list_language']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['languages_list_users']
			)
		));
		
		// Make language default?
		if ( $appCore -> _REQ( 'do') == 'default')
		{
			// New lang id
			$new_def = (integer) $appCore -> _REQ( 'lang');
			
			// New def is default?
			if ( $new_def != $appCore -> conf['default_language'])
			{
				// Make Api
				$new_lang = new api_language( $appCore, $new_def);
				
				// Style exists and is not default?
				if ( $new_lang -> data_loaded)
				{
					// Update board config
					$appCore -> setConfig( 'default_language', $new_def);	
					
					// Make Guest use this lang
					$appCore -> db -> update( array( 'user_language' => $new_def), 'users', 'user_id = \'-1\'');
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_default_language', htmlspecialchars( $this -> api -> language_name));
					
					// Show message
					$appCore -> output -> message( $appCore -> lang['acp_language_default_updated'], $appCore -> lang['acp_languages_list']);
				}
			}
			
		}
				
		// Select languages
		$appCore -> db -> query( 'SELECT l.*, COUNT( u.user_id) AS language_users
		FROM ' . DB_PREFIX . 'languages l
		LEFT JOIN ' . DB_PREFIX . 'users u ON  l.language_id = u.user_language
		GROUP BY l.language_id
		ORDER BY l.language_name');
		
		// Parse result
		while ( $language_result = $appCore -> db -> fetch_assoc())
		{	
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_longer">
					<b>' . htmlspecialchars( $language_result['language_name'] . ' (' . $language_result['language_iso'] . ')') . '</b>
				</div>
				<div class="cell left_border right_float width_medium">
					' . $appCore -> output -> toolsPopup( '<a href="' . $appCore -> adminLink( 'act=edit_lang&do=details&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['language_op_edit']) . ' ' . $appCore -> lang['language_op_edit'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=translate&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_translate', $appCore -> lang['language_op_translate']) . ' ' . $appCore -> lang['language_op_translate'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_lang&do=users&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_users', $appCore -> lang['language_op_users']) . ' ' . $appCore -> lang['language_op_users'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_lang&do=copy&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_copy', $appCore -> lang['language_op_copy']) . ' ' . $appCore -> lang['language_op_copy'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_lang&do=export&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_download', $appCore -> lang['language_op_export']) . ' ' . $appCore -> lang['language_op_export'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_lang&do=delete&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['language_op_delete']) . ' ' . $appCore -> lang['language_op_delete'] . '</a>') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					' . $appCore -> number( $language_result['language_id'] == $appCore -> conf['default_language'] ? $language_result['language_users'] - 1 : $language_result['language_users']) . '
				</div>
				<div class="cell left_border right_float width_tiny center">
					' . ( $language_result['language_id'] == $appCore -> conf['default_language'] ? $appCore -> output -> drawImage( 'item_default', $appCore -> lang['language_is_default']) : '<a href="' . $appCore -> adminLink( 'act=langs&do=default&lang=' . $language_result['language_id']) . '">' . $appCore -> output -> drawImage( 'item_not_default', $appCore -> lang['language_make_default']) . '</a>') . '
				</div>');
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_languages_list'], array( $appCore -> adminLink( 'act=langs') => $appCore -> lang['acp_languages_list']), ob_get_clean());
				
	}

}